package com.sau.pixelcart.task;

import com.sau.pixelcart.service.IOrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.scheduling.annotation.Scheduled;

@Component
@Slf4j
public class OrderScheduleTask {

    @Autowired
    IOrdersService ordersService;

    // 每天凌晨00:00执行（CRON表达式说明：秒 分 时 日 月 周）
    @Scheduled(cron = "0 0 20 * * ?", zone = "Asia/Shanghai")
    public void autoCancelUnpaidOrders() {
        log.info(" 【定时任务】开始扫描未支付订单...");
        try {
            int count = ordersService.batchCancelUnpaidOrders();
            log.info(" 【定时任务】已自动取消{}笔超时订单", count);
        } catch (Exception e) {
            log.error(" 【定时任务】订单取消异常：{}", e.getMessage());
        }
    }
}
